package 算法.练习;

import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author:谢君臣
 * @Date:2021/6/21-21:44
 * @version:1.0
 * @function:
 */
public class TestVolatile {

    public static void main(String[] args) {

        int num1 =0;
        int i = 0;
        AtomicInteger num2 = new AtomicInteger(0);

        new Thread(()->{
            while (num1<2000){
                if (Unsafe.getUnsafe().compareAndSwapInt(num1,132131,num1,num1+1))
                    System.out.println(num1);
                else
                    System.out.println("并发修改失败");
            }
        }).start();

        for (i = 0; i < 2000; i++) {
            new Thread(()->{
                num2.getAndIncrement();
            }).start();
        }
        while (Thread.activeCount()>2)
            Thread.yield();
        System.out.println(i);
        System.out.println(num1);
        System.out.println(num2);
    }



}
